﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraNavBar;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;
using MM.Common.DBBase;
using MM.Common.Base;
using DevExpress.XtraGrid.Views.Base;
using System.Drawing;
using DevExpress.Utils;
using DevExpress.XtraEditors.Controls;
using System.Drawing.Drawing2D;
using DevExpress.XtraNavBar.ViewInfo;
using MM.Common.Setup;
using MM.Commmon.UI;
using MM.Commmon.UI.Controls;
using MM.Common.UI.Controls;

namespace MM.Common.Support
{
    public partial class FrmEmployeeSetup : FrmFunctionWithTool
    {
        private DataTable dtDept;
        private DataTable dtDeptChoice;
        private DataTable dtEmpGroup;
        private DataTable dtEmpKind;
        private DataTable dtEmployee;
        private DataTable dtMenu;
        private DataTable dtRole;
        private DataTable dtRoleAll;
        private DataTable dtSubs;
        protected FrmEmp_Reg FrmER = new FrmEmp_Reg();
        protected int copyBaseEmpid = -1;
        protected int copyCashierEmpid = -1;
        protected int copyDoctorEmpid = -1;
        protected int copyRoleEmpid = -1;
        protected int copySystemEmpid = -1;
        protected bool isEdit;
        protected bool isNew;


        public FrmEmployeeSetup()
        {
            this.InitializeComponent();
            this.InitDb();
        }

        private void changeMode(bool edit)
        {
            this.isEdit = edit;
            for (int i = 0; i < this.plBase.Controls.Count; i++)
            {
                if (!(this.plBase.Controls[i] is UCLabel))
                {
                    if (this.plBase.Controls[i] is BaseEdit)
                    {
                        ((BaseEdit)this.plBase.Controls[i]).Properties.ReadOnly = !this.isEdit;
                    }
                    else
                    {
                        this.plBase.Controls[i].Enabled = edit;
                    }
                }
            }
            this.plCashier.SetEnable(edit);
            this.plDoctor.SetEnable(edit);
            this.gvList.GridControl.Enabled = !this.isEdit;
            this.nbiNew.Visible = !this.isEdit;
            this.nbiDelete.Visible = !this.isEdit && (this.gvList.FocusedRowHandle >= 0);
            this.nbiEdit.Visible = !this.isEdit && (this.gvList.FocusedRowHandle >= 0);
            this.nbiSave.Visible = this.isEdit;
            this.nbiCancel.Visible = this.isEdit;
            this.nbiRole.Visible = this.isEdit;
            this.nbiMenu.Visible = this.isEdit;
            this.gvRole.OptionsBehavior.Editable = edit;
            if (this.isEdit)
            {
                this.plBase.ContextMenuStrip = this.contextMSPaste;
                this.plCashier.ContextMenuStrip = this.contextMSPaste;
                this.plDoctor.ContextMenuStrip = this.contextMSPaste;
                this.gvRole.GridControl.ContextMenuStrip = this.contextMSPaste;
                this.gvMenu.GridControl.ContextMenuStrip = this.contextMSPaste;
            }
            else
            {
                this.plBase.ContextMenuStrip = null;
                this.plCashier.ContextMenuStrip = null;
                this.plDoctor.ContextMenuStrip = null;
                this.gvMenu.GridControl.ContextMenuStrip = null;
                this.gvRole.GridControl.ContextMenuStrip = null;
            }
        }

        private void ckCashierFlag_CheckedChanged(object sender, EventArgs e)
        {
            this.plCashier.Visible = this.ckCashierFlag.Checked;
        }

        private void ckDoctorFlag_CheckedChanged(object sender, EventArgs e)
        {
            this.plDoctor.Visible = this.ckDoctorFlag.Checked;
        }

        private void contextMSCopy_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (this.gvList.GridControl.Enabled && (this.gvList.FocusedRowHandle >= 0))
            {
                if (e.ClickedItem.Name == "toolSMICopyEmp")
                {
                    this.copyBaseEmpid = Convert.ToInt16(this.gvList.GetFocusedRowCellValue("EMP_ID"));
                    this.toolSMIPasteEmp.Visible = true;
                    this.toolSMICopyEmp.ToolTipText = "已拷贝：" + this.gvList.GetFocusedRowCellValue("EMP_NAME").ToString();
                    this.toolSMICopyEmp.AutoToolTip = true;
                    this.toolSMIPasteEmp.ToolTipText = "已拷贝：" + this.gvList.GetFocusedRowCellValue("EMP_NAME").ToString();
                    this.toolSMIPasteEmp.AutoToolTip = true;
                }
                if (e.ClickedItem.Name == "toolSMICopyDoctor")
                {
                    this.copyDoctorEmpid = Convert.ToInt16(this.gvList.GetFocusedRowCellValue("EMP_ID"));
                    this.toolSMIPasteDoctor.Visible = true;
                    this.toolSMICopyDoctor.ToolTipText = "已拷贝：" + this.gvList.GetFocusedRowCellValue("EMP_NAME").ToString();
                    this.toolSMICopyDoctor.AutoToolTip = true;
                    this.toolSMIPasteDoctor.ToolTipText = "已拷贝：" + this.gvList.GetFocusedRowCellValue("EMP_NAME").ToString();
                    this.toolSMIPasteDoctor.AutoToolTip = true;
                }
                if (e.ClickedItem.Name == "toolSMICopyCashier")
                {
                    this.copyCashierEmpid = Convert.ToInt16(this.gvList.GetFocusedRowCellValue("EMP_ID"));
                    this.toolSMIPasteCashier.Visible = true;
                    this.toolSMICopyCashier.ToolTipText = "已拷贝：" + this.gvList.GetFocusedRowCellValue("EMP_NAME").ToString();
                    this.toolSMICopyCashier.AutoToolTip = true;
                    this.toolSMIPasteCashier.ToolTipText = "已拷贝：" + this.gvList.GetFocusedRowCellValue("EMP_NAME").ToString();
                    this.toolSMIPasteCashier.AutoToolTip = true;
                }
                if (e.ClickedItem.Name == "toolSMICopyRole")
                {
                    this.copyRoleEmpid = Convert.ToInt16(this.gvList.GetFocusedRowCellValue("EMP_ID"));
                    this.toolSMIPasteRole.Visible = true;
                    this.toolSMICopyRole.ToolTipText = "已拷贝：" + this.gvList.GetFocusedRowCellValue("EMP_NAME").ToString();
                    this.toolSMICopyRole.AutoToolTip = true;
                    this.toolSMIPasteRole.ToolTipText = "已拷贝：" + this.gvList.GetFocusedRowCellValue("EMP_NAME").ToString();
                    this.toolSMIPasteRole.AutoToolTip = true;
                }
                if (e.ClickedItem.Name == "toolSMICopySystem")
                {
                    this.copySystemEmpid = Convert.ToInt16(this.gvList.GetFocusedRowCellValue("EMP_ID"));
                    this.toolSMIPasteSystem.Visible = true;
                    this.toolSMICopySystem.ToolTipText = "已拷贝：" + this.gvList.GetFocusedRowCellValue("EMP_NAME").ToString();
                    this.toolSMICopySystem.AutoToolTip = true;
                    this.toolSMIPasteSystem.ToolTipText = "已拷贝：" + this.gvList.GetFocusedRowCellValue("EMP_NAME").ToString();
                    this.toolSMIPasteSystem.AutoToolTip = true;
                }
            }
        }

        private void contextMSPaste_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if ((e.ClickedItem.Name == "toolSMIPasteEmp") && (this.copyBaseEmpid >= 0))
            {
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("empid", this.copyBaseEmpid) });
                DataTable dt = new DataTable();
                SystemInfo.DbData.Retrieve("SELECT * FROM MXH_EMPLOYEE WHERE EMP_ID = :empid", parm, ref dt);
                this.cbInputCodeChoice.EditValue = Converts.ConverToString(dt.Rows[0]["INPUT_CHOICE"], "1");
                this.cbDeptId.EditValue = Convert.ToInt16(dt.Rows[0]["DEPT_ID"]);
                this.cbEmpGroupId.EditValue = Converts.ConverToInt(dt.Rows[0]["EMP_GROUP_ID"], 0);
                this.cbEmpKindId.EditValue = Converts.ConverToInt(dt.Rows[0]["EMP_KIND_ID"], 0);
                this.ckDoctorFlag.Checked = dt.Rows[0]["DOCTOR_FLAG"].ToString() == "Y";
                this.ckCashierFlag.Checked = dt.Rows[0]["CASHIER_FLAG"].ToString() == "Y";
                this.ckAdminFlag.Checked = dt.Rows[0]["ADMIN_FLAG"].ToString() == "Y";
                this.ckUsedFlag.Checked = dt.Rows[0]["USED_FLAG"].ToString() == "Y";
            }
            if ((e.ClickedItem.Name == "toolSMIPasteDoctor") && (this.copyDoctorEmpid >= 0))
            {
                this.plDoctor.DisplayInfo(this.copyDoctorEmpid);
            }
            if ((e.ClickedItem.Name == "toolSMIPasteCashier") && (this.copyCashierEmpid >= 0))
            {
                this.plCashier.DisplayInfo(this.copyCashierEmpid);
            }
            if ((this.copyRoleEmpid < 0) || !(e.ClickedItem.Name == "toolSMIPasteRole"))
            {
                goto Label_03CA;
            }
            bool flag = true;
            if (this.dtRole.Rows.Count > 0)
            {
                switch (XtraMessageBox.Show("已有角色数据，是否清除后继续？", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
                {
                    case DialogResult.Cancel:
                        flag = false;
                        break;

                    case DialogResult.Yes:
                        this.dtRole.Rows.Clear();
                        goto Label_02A5;
                }
            }
        Label_02A5:
            if (flag)
            {
                int count = this.dtRole.Rows.Count;
                DataParamCol col2 = new DataParamCol(new DataParam[] { new InputDataParam("empid", this.copyBaseEmpid) });
                string commandtext = "SELECT R.ROLE_CODE ,R.ROLE_NAME ,E.GRANT_FLAG\r\n FROM MXHLY_SYS_ROLE_EMP E ,MXHLY_SYS_ROLE R\r\nWHERE E.ROLE_CODE = R.ROLE_CODE AND E.EMP_ID = :empid\r\nORDER BY R.ROLE_CODE";
                SystemInfo.DbData.Retrieve(commandtext, col2, ref this.dtRole);
                this.dtRole.PrimaryKey = new DataColumn[] { this.dtRole.Columns["ROLE_CODE"] };
                for (int i = 0; i < count; i++)
                {
                    for (int j = this.dtRole.Rows.Count - 1; j >= count; j--)
                    {
                        if (this.dtRole.Rows[i]["ROLE_CODE"].ToString() == this.dtRole.Rows[j]["ROLE_CODE"].ToString())
                        {
                            this.dtRole.Rows[j].Delete();
                        }
                    }
                }
            }
        Label_03CA:
            if ((this.copySystemEmpid >= 0) && (e.ClickedItem.Name == "toolSMIPasteSystem"))
            {
                bool flag2 = true;
                if (this.dtMenu.Rows.Count > 0)
                {
                    switch (XtraMessageBox.Show("已有子系统菜单数据，是否清除后继续？", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
                    {
                        case DialogResult.Cancel:
                            flag2 = false;
                            break;

                        case DialogResult.Yes:
                            this.dtMenu.Rows.Clear();
                            break;
                    }
                }
                if (flag2)
                {
                    int num4 = this.dtRole.Rows.Count;
                    DataParamCol col3 = new DataParamCol(new DataParam[] { new InputDataParam("empid", this.copyBaseEmpid) });
                    string str2 = "SELECT MXHLY_SYS_EMP_MENU.SYSTEM_CODE ,MXHLY_SYS_SYSTEM.SYSTEM_NAME ,MXHLY_SYS_EMP_MENU.ORDER_NO ,MXHLY_SYS_MENU.TITLE\r\n FROM MXHLY_SYS_EMP_MENU ,MXHLY_SYS_MENU ,MXHLY_SYS_SYSTEM \r\n WHERE MXHLY_SYS_EMP_MENU.SYSTEM_CODE = MXHLY_SYS_MENU.SYSTEM_CODE AND MXHLY_SYS_EMP_MENU.ORDER_NO = MXHLY_SYS_MENU.ORDER_NO AND \r\n    MXHLY_SYS_EMP_MENU.SYSTEM_CODE = MXHLY_SYS_SYSTEM.SYSTEM_CODE AND MXHLY_SYS_EMP_MENU.EMP_ID = :empid\r\n ORDER BY MXHLY_SYS_EMP_MENU.SYSTEM_CODE";
                    SystemInfo.DbData.Retrieve(str2, col3, ref this.dtMenu);
                    this.dtMenu.PrimaryKey = new DataColumn[] { this.dtMenu.Columns["SYSTEM_CODE"] };
                    for (int k = 0; k < num4; k++)
                    {
                        for (int m = this.dtMenu.Rows.Count - 1; m >= num4; m--)
                        {
                            if (this.dtMenu.Rows[k]["ROLE_CODE"].ToString() == this.dtMenu.Rows[m]["ROLE_CODE"].ToString())
                            {
                                this.dtMenu.Rows[m].Delete();
                            }
                        }
                    }
                }
            }
        }


        private void nbiCancel_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.changeMode(false);
            this.freshDisp();
            this.ValidationProvider.Validate();
        }

        private void nbiDelete_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if ((this.gvList.FocusedRowHandle >= 0) && (XtraMessageBox.Show("是否真的删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
            {
                int paramValue = Convert.ToInt16(this.gvList.GetFocusedRowCellValue("EMP_ID").ToString());
                string[] commandtexts = new string[5];
                DataParamCol[] parms = new DataParamCol[5];
                commandtexts[0] = "DELETE MXHLY_SYS_ROLE_EMP WHERE EMP_ID = :empid ";
                parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("empid", paramValue) });
                commandtexts[1] = "DELETE MXHLY_SYS_EMP_MENU WHERE EMP_ID = :empid ";
                parms[1] = new DataParamCol(new DataParam[] { new InputDataParam("empid", paramValue) });
                commandtexts[2] = "DELETE MXHLY_DIC_CASHIER_INFO WHERE EMP_ID = :empid ";
                parms[2] = new DataParamCol(new DataParam[] { new InputDataParam("empid", paramValue) });
                commandtexts[3] = "DELETE MXHLY_DIC_DOCTOR_INFO WHERE EMP_ID = :empid ";
                parms[3] = new DataParamCol(new DataParam[] { new InputDataParam("empid", paramValue) });
                commandtexts[4] = "DELETE MXH_EMPLOYEE WHERE EMP_ID = :empid ";
                parms[4] = new DataParamCol(new DataParam[] { new InputDataParam("empid", paramValue) });
                SystemInfo.DbData.TransactionBegin();
                if (((SystemInfo.DbData.RunSql(commandtexts, parms) == 0) && this.plDoctor.Delete(paramValue)) && this.plCashier.Delete(paramValue))
                {
                    SystemInfo.DbData.TransactionCommit();
                    this.gvList.DeleteRow(this.gvList.FocusedRowHandle);
                    this.gvList.Focus();
                    this.freshDisp();
                }
                else
                {
                    SystemInfo.DbData.TransactionRollback();
                    XtraMessageBox.Show("人员删除时不成功!\r\n" + SystemInfo.DbData.ErrorMessage);
                }
            }
        }

        private void nbiEdit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.changeMode(true);
            this.isNew = false;
        }

        private void nbiExit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            base.Close();
        }

        private void nbiMenu_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (this.isEdit && (this.nbiMenu.Visible && this.nbiMenu.Enabled))
            {
                string str = this.txtEmpCode.Text + this.txtEmpName.Text;
                new FrmEmpMenuSetup(this.dtMenu) { Text = "默认菜单设置(" + str + ")" }.ShowDialog(this);
            }
        }

        private void nbiNew_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.txtEmpCode.Text = "";
            this.txtEmpName.Text = "";
            this.txtPassword.Text = "";
            this.cbInputCodeChoice.EditValue = "1";
            this.txtInputCode1.Text = "";
            this.txtInputCode2.Text = "";
            this.cbDeptId.EditValue = 0;
            this.cbEmpGroupId.EditValue = 0;
            this.cbEmpKindId.EditValue = 0;
            this.ckDoctorFlag.Checked = false;
            this.ckCashierFlag.Checked = false;
            this.ckAdminFlag.Checked = false;
            this.ckUsedFlag.Checked = true;
            this.plCashier.New();
            this.plDoctor.New();
            this.changeMode(true);
            this.dtRole.Rows.Clear();
            this.dtMenu.Rows.Clear();
            this.isNew = true;
        }

        private void nbiOrgReg_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.FrmER.Edit(Convert.ToInt32(this.gvList.GetFocusedRowCellValue("EMP_ID")));
        }

        private void nbiRole_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (this.isEdit && (this.nbiRole.Visible && this.nbiRole.Enabled))
            {
                if (this.dtRoleAll.Rows.Count == 0)
                {
                    SystemInfo.DbData.Retrieve("SELECT ROLE_CODE ,ROLE_NAME ,'N' GRANT_FLAG FROM MXHLY_SYS_ROLE ORDER BY ROLE_CODE", ref this.dtRoleAll);
                    this.dtRoleAll.PrimaryKey = new DataColumn[] { this.dtRoleAll.Columns["ROLE_CODE"] };
                }
                int[] numArray = new int[3];
                numArray[1] = 100;
                new FrmMulSelFromGrid(this.dtRoleAll, new string[] { "代码", "名称", "扩散权" }, numArray, "报表角色选择", ref this.dtRole).ShowDialog(this);
            }
        }

        private void nbiSave_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (this.ValidationProvider.Validate())
            {
                string[] strArray;
                DataParamCol[] colArray;
                int paramValue = 0;
                string text = this.txtEmpCode.Text;
                string str2 = this.txtEmpName.Text;
                string str3 = new Password().Encrypting(this.txtPassword.Text.Trim());
                string str4 = this.cbInputCodeChoice.EditValue.ToString();
                string str5 = this.txtInputCode1.Text;
                string str6 = this.txtInputCode2.Text;
                object obj2 = Converts.SetDbInt(this.cbDeptId.EditValue);
                object obj3 = Converts.SetDbInt(this.cbEmpGroupId.EditValue);
                object obj4 = Converts.SetDbInt(this.cbEmpKindId.EditValue);
                string str7 = this.ckDoctorFlag.Checked ? "Y" : "N";
                string str8 = this.ckCashierFlag.Checked ? "Y" : "N";
                string str9 = this.ckAdminFlag.Checked ? "Y" : "N";
                string str10 = this.ckUsedFlag.Checked ? "Y" : "N";
                int index = 0;
                this.dtRole.AcceptChanges();
                this.dtMenu.AcceptChanges();
                if (this.isNew)
                {
                    strArray = new string[(this.dtMenu.Rows.Count + this.dtRole.Rows.Count) + 1];
                    colArray = new DataParamCol[(this.dtMenu.Rows.Count + this.dtRole.Rows.Count) + 1];
                    object obj5 = null;
                    SystemInfo.DbData.ExecuteScalar("SELECT MAX(EMP_ID) FROM MXH_EMPLOYEE", ref obj5);
                    if (obj5 != DBNull.Value)
                    {
                        paramValue = Convert.ToInt16(obj5) + 1;
                    }
                    else
                    {
                        paramValue = 1;
                    }
                    strArray[0] = "INSERT INTO MXH_EMPLOYEE (EMP_ID ,EMP_CODE ,EMP_NAME ,PASSWORD ,INPUT_CHOICE ,INPUTCODE1 ,INPUTCODE2 ,DEPT_ID ,EMP_GROUP_ID ,EMP_KIND_ID ,DOCTOR_FLAG ,CASHIER_FLAG ,ADMIN_FLAG ,USED_FLAG)\r\n   VALUES (:empid ,:empcode ,:empname ,:pwd ,:input_choice ,:inputcode1 ,:inputcode2 ,:deptid ,:groupid ,:kindid ,:doctor_flag ,:cashier_flag ,:admin_flag ,:used_flag)";
                    colArray[0] = new DataParamCol(new DataParam[] { new InputDataParam("empid", paramValue), new InputDataParam("empcode", text), new InputDataParam("empname", str2), new InputDataParam("pwd", str3), new InputDataParam("input_choice", str4), new InputDataParam("inputcode1", str5), new InputDataParam("inputcode2", str6), new InputDataParam("deptid", obj2), new InputDataParam("groupid", obj3), new InputDataParam("kindid", obj4), new InputDataParam("doctor_flag", str7), new InputDataParam("cashier_flag", str8), new InputDataParam("admin_flag", str9), new InputDataParam("used_flag", str10) });
                    index = 1;
                }
                else
                {
                    strArray = new string[(this.dtMenu.Rows.Count + this.dtRole.Rows.Count) + 3];
                    colArray = new DataParamCol[(this.dtMenu.Rows.Count + this.dtRole.Rows.Count) + 3];
                    paramValue = Convert.ToInt16(this.gvList.GetFocusedRowCellValue("EMP_ID"));
                    strArray[0] = "UPDATE MXH_EMPLOYEE SET EMP_CODE = :emp_code ,EMP_NAME = :emp_name ,PASSWORD = :pwd ,INPUT_CHOICE = :input_choice , INPUTCODE1 = :inputcode1 ,INPUTCODE2 = :inputcode2 ,DEPT_ID = :deptid ,EMP_GROUP_ID = :groupid ,EMP_KIND_ID = :kindid , DOCTOR_FLAG = :doctor_flag ,CASHIER_FLAG = :cashier_flag ,ADMIN_FLAG = :admin_flag ,USED_FLAG = :used_flag  \r\n     WHERE EMP_ID = :emp_id ";
                    colArray[0] = new DataParamCol(new DataParam[] { new InputDataParam("emp_code", text), new InputDataParam("emp_name", str2), new InputDataParam("pwd", str3), new InputDataParam("input_choice", str4), new InputDataParam("inputcode1", str5), new InputDataParam("inputcode2", str6), new InputDataParam("deptid", obj2), new InputDataParam("groupid", obj3), new InputDataParam("kindid", obj4), new InputDataParam("doctor_flag", str7), new InputDataParam("cashier_flag", str8), new InputDataParam("admin_flag", str9), new InputDataParam("used_flag", str10), new InputDataParam("emp_id", paramValue) });
                    strArray[1] = "DELETE FROM MXHLY_SYS_ROLE_EMP WHERE EMP_ID = :empid";
                    colArray[1] = new DataParamCol(new DataParam[] { new InputDataParam("empid", paramValue) });
                    strArray[2] = "DELETE FROM MXHLY_SYS_EMP_MENU WHERE EMP_ID = :empid";
                    colArray[2] = new DataParamCol(new DataParam[] { new InputDataParam("empid", paramValue) });
                    index = 3;
                }
                int num3 = 0;
                while (num3 < this.dtRole.Rows.Count)
                {
                    strArray[index] = "INSERT INTO MXHLY_SYS_ROLE_EMP (EMP_ID ,ROLE_CODE ,GRANT_FLAG) VALUES (:empid ,:rolecode ,:grantflag)";
                    colArray[index] = new DataParamCol(new DataParam[] { new InputDataParam("empid", paramValue), new InputDataParam("rolecode", this.dtRole.Rows[num3]["ROLE_CODE"].ToString()), new InputDataParam("grantflag", this.dtRole.Rows[num3]["GRANT_FLAG"].ToString()) });
                    num3++;
                    index++;
                }
                int num4 = 0;
                while (num4 < this.dtMenu.Rows.Count)
                {
                    strArray[index] = "INSERT INTO MXHLY_SYS_EMP_MENU (SYSTEM_CODE ,EMP_ID ,ORDER_NO) VALUES (:sscode ,:empid ,:seqcode)";
                    colArray[index] = new DataParamCol(new DataParam[] { new InputDataParam("sscode", this.dtMenu.Rows[num4]["SYSTEM_CODE"].ToString()), new InputDataParam("empid", paramValue), new InputDataParam("seqcode", this.dtMenu.Rows[num4]["ORDER_NO"]) });
                    num4++;
                    index++;
                }
                SystemInfo.DbData.TransactionBegin();
                if (((SystemInfo.DbData.RunSql(strArray, colArray) == 0) && this.plDoctor.Save(paramValue, str7 == "Y")) && this.plCashier.Save(paramValue, str8 == "Y"))
                {
                    SystemInfo.DbData.TransactionCommit();
                    if (this.isNew)
                    {
                        DataRow row = this.dtEmployee.NewRow();
                        row["EMP_ID"] = paramValue;
                        row["EMP_CODE"] = text;
                        row["EMP_NAME"] = str2;
                        row["USED_FLAG"] = str10;
                        row["DEPT_ID"] = obj2;
                        this.gvList.ClearSorting();
                        this.dtEmployee.Rows.Add(row);
                        this.gvList.FocusedRowHandle = this.gvList.RowCount - 1;
                    }
                    else
                    {
                        this.gvList.SetFocusedRowCellValue("EMP_ID", paramValue);
                        this.gvList.SetFocusedRowCellValue("EMP_CODE", text);
                        this.gvList.SetFocusedRowCellValue("EMP_NAME", str2);
                        this.gvList.SetFocusedRowCellValue("USED_FLAG", str10);
                        this.gvList.SetFocusedRowCellValue("DEPT_ID", obj2);
                    }
                    this.changeMode(false);
                    this.freshDisp();
                }
                else
                {
                    SystemInfo.DbData.TransactionRollback();
                    XtraMessageBox.Show("更新不成功！\n原因：" + SystemInfo.DbData.ErrorMessage, "出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
        }

        private void txtXm_TextChanged(object sender, EventArgs e)
        {
            this.txtInputCode1.Text = IME.GetInputCode(this.txtEmpName.Text, "1");
            this.txtInputCode2.Text = IME.GetInputCode(this.txtEmpName.Text, "2");
        }

        private void freshDisp()
        {
            if (this.gvList.FocusedRowHandle >= 0)
            {
                int paramValue = Convert.ToInt16(this.gvList.GetFocusedRowCellValue("EMP_ID").ToString());
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("empid", paramValue) });
                DataTable dt = new DataTable();
                SystemInfo.DbData.Retrieve("SELECT * FROM MXH_EMPLOYEE WHERE EMP_ID = :empid", parm, ref dt);
                this.txtEmpCode.Text = dt.Rows[0]["EMP_CODE"].ToString();
                this.txtEmpName.Text = dt.Rows[0]["EMP_NAME"].ToString();
                Password password = new Password();
                try
                {
                    this.txtPassword.Text = password.Decrypting(dt.Rows[0]["PASSWORD"].ToString());
                }
                catch
                {
                    this.txtPassword.Text = "";
                }
                this.cbInputCodeChoice.Text = Converts.ConverToString(dt.Rows[0]["INPUT_CHOICE"], "1");
                this.txtInputCode1.Text = Converts.ConverToString(dt.Rows[0]["INPUTCODE1"], "");
                this.txtInputCode2.Text = Converts.ConverToString(dt.Rows[0]["INPUTCODE2"], "");
                this.cbDeptId.Properties.ReadOnly = false;
                this.cbEmpGroupId.Properties.ReadOnly = false;
                this.cbEmpKindId.Properties.ReadOnly = false;
                this.cbDeptId.ItemIndex = this.cbDeptId.Properties.GetDataSourceRowIndex("DEPT_ID", Convert.ToInt16(dt.Rows[0]["DEPT_ID"]));
                if (dt.Rows[0]["EMP_GROUP_ID"].ToString().IsInt())
                {
                    this.cbEmpGroupId.ItemIndex = this.cbEmpGroupId.Properties.GetDataSourceRowIndex("EMP_GROUP_ID", Convert.ToInt16(dt.Rows[0]["EMP_GROUP_ID"]));
                }
                else
                {
                    this.cbEmpGroupId.ItemIndex = this.cbEmpGroupId.Properties.GetDataSourceRowIndex("EMP_GROUP_ID", null);
                }
                this.cbEmpKindId.ItemIndex = this.cbEmpKindId.Properties.GetDataSourceRowIndex("DATA_CODE", dt.Rows[0]["EMP_KIND_ID"]);
                this.cbDeptId.Properties.ReadOnly = true;
                this.cbEmpGroupId.Properties.ReadOnly = true;
                this.cbEmpKindId.Properties.ReadOnly = true;
                this.ckDoctorFlag.Checked = dt.Rows[0]["DOCTOR_FLAG"].ToString() == "Y";
                this.ckCashierFlag.Checked = dt.Rows[0]["CASHIER_FLAG"].ToString() == "Y";
                this.ckAdminFlag.Checked = dt.Rows[0]["ADMIN_FLAG"].ToString() == "Y";
                this.ckUsedFlag.Checked = dt.Rows[0]["USED_FLAG"].ToString() == "Y";
                this.plDoctor.DisplayInfo(paramValue);
                this.plCashier.DisplayInfo(paramValue);
                this.plDoctor.Visible = this.ckDoctorFlag.Checked;
                this.plCashier.Visible = this.ckCashierFlag.Checked;
                string commandtext = "SELECT R.ROLE_CODE ,R.ROLE_NAME ,E.GRANT_FLAG\r\n FROM MXHLY_SYS_ROLE_EMP E ,MXHLY_SYS_ROLE R\r\nWHERE E.ROLE_CODE = R.ROLE_CODE AND E.EMP_ID = :empid\r\nORDER BY R.ROLE_CODE";
                this.dtRole.Rows.Clear();
                SystemInfo.DbData.Retrieve(commandtext, parm, ref this.dtRole);
                this.dtRole.PrimaryKey = new DataColumn[] { this.dtRole.Columns["ROLE_CODE"] };
                this.dtRole.Columns["GRANT_FLAG"].DefaultValue = "N";
                commandtext = "SELECT MXHLY_SYS_EMP_MENU.SYSTEM_CODE ,MXHLY_SYS_SYSTEM.SYSTEM_NAME ,MXHLY_SYS_EMP_MENU.ORDER_NO ,MXHLY_SYS_MENU.TITLE\r\n FROM MXHLY_SYS_EMP_MENU ,MXHLY_SYS_MENU ,MXHLY_SYS_SYSTEM \r\n WHERE MXHLY_SYS_EMP_MENU.SYSTEM_CODE = MXHLY_SYS_MENU.SYSTEM_CODE AND MXHLY_SYS_EMP_MENU.ORDER_NO = MXHLY_SYS_MENU.ORDER_NO AND \r\n    MXHLY_SYS_EMP_MENU.SYSTEM_CODE = MXHLY_SYS_SYSTEM.SYSTEM_CODE AND MXHLY_SYS_EMP_MENU.EMP_ID = :empid\r\n ORDER BY MXHLY_SYS_EMP_MENU.SYSTEM_CODE";
                this.dtMenu.Rows.Clear();
                SystemInfo.DbData.Retrieve(commandtext, parm, ref this.dtMenu);
                this.dtMenu.PrimaryKey = new DataColumn[] { this.dtMenu.Columns["SYSTEM_CODE"] };
                this.plBase.Visible = true;
            }
            else
            {
                this.plBase.Visible = false;
                this.plDoctor.Visible = false;
                this.plCashier.Visible = false;
                this.dtRole.Rows.Clear();
                this.dtMenu.Rows.Clear();
            }
        }

        private void FrmEmployeeSetup_Shown(object sender, EventArgs e)
        {
            this.changeMode(false);
            if (this.dtEmployee.Rows.Count > 0)
            {
                this.freshDisp();
            }
        }

        private void gvList_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (this.gvList.FocusedRowHandle >= 0)
            {
                this.freshDisp();
            }
        }

        private void gvMenu_DoubleClick(object sender, EventArgs e)
        {
            this.nbiMenu_LinkClicked(this.nbiMenu, new NavBarLinkEventArgs(new NavBarItemLink(this.nbiMenu)));
        }

        private void gvRole_DoubleClick(object sender, EventArgs e)
        {
            this.nbiRole_LinkClicked(this.nbiRole, new NavBarLinkEventArgs(new NavBarItemLink(this.nbiRole)));
        }

        private void InitDb()
        {
            this.dtEmployee = new DataTable();
            this.dtDeptChoice = new DataTable();
            this.dtDept = new DataTable();
            this.dtEmpGroup = new DataTable();
            this.dtEmpKind = new DataTable();
            this.dtSubs = new DataTable();
            this.dtRoleAll = new DataTable();
            this.dtRole = new DataTable();
            this.dtMenu = new DataTable();
            this.gvRole.GridControl.DataSource = this.dtRole;
            this.gvMenu.GridControl.DataSource = this.dtMenu;
            DataTable[] dts = new DataTable[4];
            string[] commandtexts = new string[4];
            DataParamCol[] parms = new DataParamCol[4];
            commandtexts[0] = "SELECT DEPT_ID ,DEPT_CODE ,INPUTCODE1 ,INPUTCODE2 ,DEPT_NAME FROM MXHLY_DIC_DEPT WHERE USED_FLAG = 'Y' ORDER BY DEPT_CODE";
            parms[0] = new DataParamCol();
            dts[0] = this.dtDept;
            commandtexts[1] = "SELECT EMP_GROUP_ID ,EMP_GROUP_CODE ,INPUTCODE1 ,INPUTCODE2 ,EMP_GROUP_NAME FROM MXHLY_DIC_EMP_GROUP WHERE USED_FLAG = 'Y' ORDER BY EMP_GROUP_CODE";
            parms[1] = new DataParamCol();
            dts[1] = this.dtEmpGroup;
            commandtexts[2] = "SELECT DATA_CODE ,DATA_VALUE ,INPUTCODE1 ,INPUTCODE2 FROM MXHLY_DIC_COMMON_DATA WHERE DATA_TYPE = :datatype AND USED_FLAG = 'Y' AND DATA_CODE <> '*' ORDER BY DATA_CODE";
            parms[2] = new DataParamCol(new DataParam[] { new InputDataParam("datatype", "EMP_KIND") });
            dts[2] = this.dtEmpKind;
            commandtexts[3] = "SELECT EMP_ID ,EMP_CODE ,EMP_NAME ,USED_FLAG ,DEPT_ID FROM MXH_EMPLOYEE ORDER BY EMP_CODE";
            parms[3] = new DataParamCol();
            dts[3] = this.dtEmployee;
            SystemInfo.DbData.Retrieve(commandtexts, parms, ref dts);
            this.dtDeptChoice = this.dtDept.Copy();
            this.cbDeptId.Properties.DataSource = this.dtDept;
            this.cbDeptId.Properties.ValueMember = "DEPT_ID";
            this.cbDeptId.Properties.DisplayMember = "DEPT_NAME";
            if (this.dtDept.Rows.Count == 0)
            {
                XtraMessageBox.Show("部门未设置！", "提示");
            }
            if ((this.dtEmpGroup != null) && (this.dtEmpGroup.Rows.Count > 0))
            {
                DataRow row = this.dtEmpGroup.NewRow();
                row["EMP_GROUP_ID"] = Convert.DBNull;
                row["EMP_GROUP_NAME"] = "未分组";
                this.dtEmpGroup.Rows.InsertAt(row, 0);
            }
            this.cbEmpGroupId.Properties.DataSource = this.dtEmpGroup;
            this.cbEmpGroupId.Properties.ValueMember = "EMP_GROUP_ID";
            this.cbEmpGroupId.Properties.DisplayMember = "EMP_GROUP_NAME";
            if (this.dtEmpGroup.Rows.Count == 0)
            {
                XtraMessageBox.Show("职工分组未设置！", "提示");
            }
            this.cbEmpKindId.Properties.DataSource = this.dtEmpKind;
            this.cbEmpKindId.Properties.DisplayMember = "DATA_VALUE";
            this.cbEmpKindId.Properties.ValueMember = "DATA_CODE";
            if (this.dtEmpKind.Rows.Count == 0)
            {
                XtraMessageBox.Show("职工类型未设置！", "提示");
            }
            this.riDept.DataSource = this.dtDeptChoice;
            this.gvList.GridControl.DataSource = this.dtEmployee;
            this.gvList.FocusedRowChanged += new FocusedRowChangedEventHandler(this.gvList_FocusedRowChanged);
        }
    }
}
